#include "fxcc/core/graph/common/AnimationController.h"

using namespace fxcc::graph::common;

bool AnimationController::UpdateEntityMap(float deltaTime)
{
	if (m_TransitionWorking)
	{
		return UpdateEntityMapTransition(deltaTime);
	}
	else
	{
		return UpdateEntityMapAnim(deltaTime);
	}
}

bool AnimationController::UpdateEntityMapAnim(float deltaTime)
{
	auto animIt = m_AnimationNodes.find(m_AnimId);
	if (animIt == m_AnimationNodes.end())
	{
		return false;
	}

	auto& anim = animIt->second;
	anim.Tick(deltaTime);

	return true;
}

bool AnimationController::UpdateEntityMapTransition(float deltaTime)
{
	auto transitionIt = m_AnimTransitions.find(m_TransitionId);;
	if (transitionIt == m_AnimTransitions.end())
	{
		return false;
	}

	auto& transition = transitionIt->second;
	transition.UpdateEntityMap();

	return true;
}

bool AnimationController::SetEntityMap()
{
	if (m_TransitionWorking)
	{
		return SetEntityMapTransition();
	}
	else
	{
		return SetEntityMapAnim();
	}
}

bool AnimationController::SetEntityMapAnim()
{
	if (!HasAnim(m_AnimId))
	{
		return false;
	}

	auto animIt = m_AnimationNodes.find(m_AnimId);
	animIt->second.UpdateBoneData();

	return true;
}

bool AnimationController::SetEntityMapTransition()
{
	if (!HasTransition(m_TransitionId))
	{
		return false;
	}
	auto transitionIt = m_AnimTransitions.find(m_TransitionId);

	return true;
}


//void AnimationController::OnLoad(Entity* entity)
//{
//	m_AnimId = m_StartAnimId;
//
//	//for (auto& [key, anim] : m_AnimationNodes)
//	//{
//	//	anim.OnLoad(entity);
//	//};
//
//	for (auto& [key1, value1] : m_AnimTransitions)
//	{
//		int leftAnimId = value1.m_LeftId;
//		int rightAnimId = value1.m_RightId;
//
//		if (HasAnim(leftAnimId) && HasAnim(rightAnimId))
//		{
//			value1.m_LeftAnim = m_AnimationNodes[leftAnimId];
//			value1.m_RightAnim = m_AnimationNodes[rightAnimId];
//
//		
//			value1.InitLeftAndRight();
//			
//		}
//
//
//	}
//}

bool AnimationController::HasAnim(const int& id)
{
	auto animNodeIt = m_AnimationNodes.find(id);;
	return animNodeIt != m_AnimationNodes.end();
}

bool AnimationController::HasTransition(const int& id)
{
	auto transitionIt = m_AnimTransitions.find(id);;
	return transitionIt != m_AnimTransitions.end();
}

const std::vector<int> AnimationController::AllAnimIds() const
{
	std::vector<int> res;
	for (auto [key, value] : m_AnimationNodes)
	{
		res.push_back(key);
	}
	return res;
}

const std::vector<int> AnimationController::AllTransitionIds() const
{
	std::vector<int> res;

	for (auto [key, value] : m_AnimTransitions)
	{
		res.push_back(key);
	}
	return res;
}


